package com.ljx.recruitment.user.util;

import cn.hutool.json.JSONUtil;
import com.ljx.recruitment.interview.constant.JobConstant;
import com.ljx.recruitment.interview.domain.dto.TitleDetailsDto;
import com.ljx.recruitment.user.constant.EmailConstant;

import java.util.Map;
import java.util.regex.Pattern;

/**
 * @Author: LiJunXi
 * @Description:
 * @Date: 2024-12-28  16:42
 */
public class EmailUtil {

    public static String getThanksContent(Map<String, String> params) {
        return String.format(EmailConstant.EMAIL_THANKS_STAGE_CONTENT, params.get("name"), params.get("job"),
                getTitleDetail(params).getOldTitle(), params.get("tenantName"));
    }

    public static String getInterviewContent(Map<String, String> params) {
        return String.format(EmailConstant.EMAIL_INTERVIEW_STAGE_CONTENT, params.get("name"), params.get("job"),
                getTitleDetail(params).getOldTitle(), getTitleDetail(params).getNewTitle(), params.get("method"),
                params.get("location"), params.get("beginTime"), params.get("endTime"), params.get("tenantName"));
    }

    public static String getWrittenContent(Map<String, String> params) {
        return String.format(EmailConstant.EMAIL_WRITTEN_STAGE_CONTENT, params.get("name"), params.get("job"),
                getTitleDetail(params).getOldTitle(), getTitleDetail(params).getNewTitle(),
                params.get("url"), params.get("beginTime"), params.get("endTime"), params.get("tenantName"));
    }

    public static String getResumeContent(Map<String, String> params) {
        return String.format(EmailConstant.EMAIL_RESUME_STAGE_CONTENT, params.get("name"), params.get("job"),
                getTitleDetail(params).getOldTitle(), getTitleDetail(params).getNewTitle(), params.get("tenantName"));
    }

    public static String getWaitOfferContent(Map<String, String> params) {
        return String.format(EmailConstant.EMAIL_WAIT_OFFER_STAGE_CONTENT, params.get("name"), params.get("job"), params.get("tenantName"));
    }

    public static String getOfferContent(Map<String, String> params) {
        return String.format(EmailConstant.EMAIL_OFFER_STAGE_CONTENT, params.get("name"), params.get("job"), params.get("job"),
                params.get("beginTime"), params.get("salary"), params.get("others"), params.get("tenantName"));
    }

    public static TitleDetailsDto getTitleDetail(Map<String, String> params) {
        return JSONUtil.toBean(params.get(JobConstant.EMAIL_INTERVIEW_TITLE_DETAILS), TitleDetailsDto.class);
    }

    public static String getSalvageContent(Map<String, String> params) {
        return String.format(EmailConstant.EMAIL_SALVAGE_STAGE_CONTENT, params.get("name"), params.get("job"), params.get("job"), params.get("tenantName"));
    }

    private static final String EMAIL_REGEX =
            "^[a-zA-Z0-9_+&*-]+(?:\\.[a-zA-Z0-9_+&*-]+)*@" +
                    "(?:[a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,7}$";

    private static final Pattern pattern = Pattern.compile(EMAIL_REGEX);
    public static boolean isValid(String email) {
        if (email == null || email.isEmpty()) {
            return false;
        }
        return pattern.matcher(email).matches();
    }
}
